<template>
  <a-spin :spinning="confirmLoading">
    <a-form-model ref="form" :model="model" :rules="validatorRules">
      <a-row>
        <a-col style="margin-bottom: 15px" v-for="(item, index) in renewalRules" :key="index">
          <i18n path="renewal-rules" tag="span">
            <a-input
              place="cash"
              v-model="item.price"
              :disabled="item.disable && !disableSubmit"
              style="width: 70px; margin: 0 5px"
            ></a-input>
            <j-dict-select-tag
              place="type"
              v-model="item.type"
              dictCode="bis_member_rule_renewal.type"
              :disabled="item.disable && !disableSubmit"
              style="width: 100px; margin: 0 5px"
            ></j-dict-select-tag>
            <a-input
              place="cash"
              v-model="item.price"
              :disabled="item.disable && !disableSubmit"
              style="width: 70px; margin: 0 5px"
            ></a-input>
            <a-input
              place="month"
              v-model="item.renewal"
              :disabled="item.disable && !disableSubmit"
              style="width: 80px; margin: 0 5px"
            ></a-input>
            <a-input
              place="coin"
              v-model="item.coin"
              :disabled="item.disable && !disableSubmit"
              style="width: 80px; margin: 0 5px"
            ></a-input>
            <a-input
              place="ticket"
              v-model="item.lottery"
              :disabled="item.disable && !disableSubmit"
              style="width: 70px; margin: 0 5px"
            ></a-input>
            <a-input
              place="credit"
              v-model="item.integral"
              :disabled="item.disable && !disableSubmit"
              style="width: 70px; margin: 0 5px"
            ></a-input>
          </i18n>
          <a href="javascript:void(0);" style="margin-left: 8px" v-if="item.disable && !disableSubmit"
            ><a-icon type="edit" @click="editRenewalRule(index)"
          /></a>
          <a href="javascript:void(0);" style="margin-left: 8px" v-if="!item.disable && !disableSubmit"
            ><a-icon type="check" @click="saveRenewalRule(item, index)"
          /></a>
          <a href="javascript:void(0);" style="margin-left: 8px" v-if="!disableSubmit"
            ><a-icon type="close" @click="deleteRenewalRule(item, index)"
          /></a>
          <a href="javascript:void(0);" style="margin-left: 8px" v-if="index == renewalRules.length - 1 && !disableSubmit"
            ><a-icon type="plus" @click="addRenewalRule()"
          /></a>
        </a-col>
      </a-row>
    </a-form-model>
  </a-spin>
</template>

<script>
import { isEmpty1 } from '@/api/comm'
import { httpAction, getAction, deleteAction } from '@/api/manage'

export default {
  name: 'RuleRenewalForm',
  components: {
  },
  data () {
    return {
      model:{},
      disableSubmit: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
      },
      url: {
        add: "/member/bisMemberRuleRenewal/add",
        edit: "/member/bisMemberRuleRenewal/edit",
        delete: "/member/bisMemberRuleRenewal/delete",
        queryById: "/member/bisMemberRuleRenewal/queryById"
      },
      renewalRules: [],
    }
  },
  created () {
     //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    edit(record) {
      this.visible = true
      this.model = Object.assign({}, record)
      this.getRenewalRule()
    },
    getRenewalRule() {
      let that = this
      getAction('/member/bisMemberRuleRenewal/list', { memberTypeId: that.model.id, pageSize: 1000 }).then((res) => {
        if (res.success) {
          let records = res.result.records
          records.forEach((rd) => {
            rd.disable = true
          })
          that.renewalRules = records
        } else {
          that.$message.error(res.message)
        }
        if (!that.disableSubmit) {
          that.renewalRules.push({
            memberTypeId: that.model.id,
            disable: false,
            price: undefined,
            type: 0,
            renewal: undefined,
            coin: undefined,
            lottery: undefined,
            integral: undefined,
          })
        }
      })
    },
    addRenewalRule() {
      this.renewalRules.push({
        memberTypeId: this.model.id,
        price: undefined,
        type: 0,
        renewal: undefined,
        coin: undefined,
        lottery: undefined,
        integral: undefined,
        disable: false,
      })
    },
    deleteRenewalRule(item, i) {
      let that = this
      if (item.id) {
        deleteAction(that.url.delete, { id: item.id }).then((res) => {
          if (res.success) {
            that.$message.success(res.message)
            that.getRenewalRule()
          } else {
            that.$message.warning(res.message)
          }
        })
      } else if (that.renewalRules.length - 1 > 0) {
        that.renewalRules.splice(i, 1)
      }
    },
    editRenewalRule(i) {
      this.$set(this.renewalRules[i], 'disable', false)
    },
    saveRenewalRule(item, i) {
      let that = this
      if (isEmpty1(item.price)) {
        that.$message.warn(that.$t('请输入付款值'))
      } else if (isNaN(item.price)) {
        that.$message.warn(that.$t('付款值必须是数值'))
      } else if (item.type === undefined || item.type === '') {
        that.$message.warn(that.$t('请选择续期类型'))
      } else if (item.renewal === '0' || item.renewal === undefined || item.renewal === '') {
        that.$message.warn(that.$t('请输入续期值'))
      } else if (isNaN(item.renewal)) {
        that.$message.warn(that.$t('续期值必须是数值'))
      } else {
        that.$set(that.renewalRules[i], 'disable', true)
        if (isEmpty1(item.coin)) {
          item.coin = '0'
        }
        if (isEmpty1(item.lottery)) {
          item.lottery = '0'
        }
        if (isEmpty1(item.integral)) {
          item.integral = '0'
        }
        let httpurl = ''
        let method = ''
        if (!item.id) {
          httpurl += that.url.add
          method = 'post'
        } else {
          httpurl += that.url.edit
          method = 'put'
        }
        let query = Object.assign({}, item)
        httpAction(httpurl, query, method).then((res) => {
          if (res.success) {
            that.$set(item, 'disabled', true)
            that.getRenewalRule()
            that.$message.success(res.message)
          } else {
            that.$message.warning(res.message)
          }
        })
      }
    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
.cus-form {
  margin-top: 10px;
  margin-left: 40px;
}
</style>